package com.resoft.bigdata.p2p.record;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.hbase.util.Bytes;

import com.resoft.bigdata.util.DateUtil;


public abstract class AbsRecord {
	public static final byte[]  ORG_CODE= Bytes.toBytes("ORGCODE");
	public static final byte[]  ORG_NAME= Bytes.toBytes("ORGNAME");
	public static final byte[]  DATADATE= Bytes.toBytes("DATADATE");
	public static final byte[]  REPORTER= Bytes.toBytes("REPORTER");
	public static final byte[]  CHECKER= Bytes.toBytes("CHECKER");
	public static final byte[]  BANK_ID= Bytes.toBytes("DBANK_ID");
	public static final byte[]  DATE= Bytes.toBytes("DDATE");
	public static final byte[]  XH= Bytes.toBytes("XH");
	public static final byte[]  XMHT= Bytes.toBytes("XMHT");
	
	
	protected byte[] storedRowKey;
	protected String orgCode; //p2p 机构代码 
	protected String orgName; //p2p  机构名称
	protected String dataDate; // 上报日期
	protected String reporter; // 上报人员
	protected String checker; // 检验人员
	protected String bankId; //机构编码或名称，不确定
	protected String ddate; //上报日期，即业务放开日期
	protected String xh; //序号
	protected String xmht; //项目合同编号
			
	public void setStoredRowKey(byte[] rowKey) {
		this.storedRowKey = rowKey;
	}

	public byte[] getStoredRowKey(){
		return storedRowKey;
	}
	
	
	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getDataDate() {
		return dataDate;
	}

	public void setDataDate(String dataDate) {
		this.dataDate = dataDate;
	}

	public String getReporter() {
		return reporter;
	}

	public void setReporter(String reporter) {
		this.reporter = reporter;
	}

	public String getChecker() {
		return checker;
	}

	public void setChecker(String checker) {
		this.checker = checker;
	}

	public String getBankId() {
		return bankId;
	}

	public void setBankId(String bankId) {
		this.bankId = bankId;
	}

	public String getDdate() {
		return ddate;
	}

	public void setDdate(String ddate) {
		this.ddate = ddate;
	}

	public String getXh() {
		return xh;
	}

	public void setXh(String xh) {
		this.xh = xh;
	}

	public String getXmht() {
		return xmht;
	}

	public void setXmht(String xmht) {
		this.xmht = xmht;
	}
	
	public byte[] getRowKey() throws Exception {
	/*
	 *  * 行键设计为：p2p机构代码 +  日期 + 序号
	 * 
	*/		
		return getRowKey(orgCode, ddate, Integer.parseInt(xh));
	}
	
	public static byte[] getRowKey(String orgCode, String dateString, int xh) throws Exception{
		byte [] buffer = new byte[12];
		Bytes.putInt(buffer, 0, orgCode.hashCode());
		int days = DateUtil.reverseDate(dateString);
		if(days == Integer.MAX_VALUE){
			throw  new Exception("Bad date");
		}
		Bytes.putInt(buffer, 4, days);
		Bytes.putInt(buffer, 8, xh);
		return buffer ;
	}
	

	public void setValue(byte[] key, byte[] value){
		Method methods[] = this.getClass().getMethods();
		String methodName = "set" + Bytes.toString(key);
		String valueString = Bytes.toString(value);
		for(int i = 0; i < methods.length; i++){
			if(methods[i].getName().equalsIgnoreCase(methodName)){
				try {
					methods[i].invoke(this, valueString);
				} catch (IllegalAccessException e) {
					System.err.println("IllegalAccessException，"   + ", Key:" + key + ", Value:" + value);
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					System.err.println("IllegalArgumentException，"  + ", Key:" + key + ", Value:" + value);
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					System.err.println("InvocationTargetException，"   + ", Key:" + key + ", Value:" + value);
					e.printStackTrace();
				}
			}
			
		}
	}
	
	public Map<byte[], byte[]> getCells(){
		Map<byte[], byte[]> cells = new HashMap<byte[], byte[]>();
		
		if(orgCode != null) cells.put(ORG_CODE, Bytes.toBytes(orgCode));
		if(orgName != null) cells.put(ORG_NAME, Bytes.toBytes(orgName));
		if(dataDate != null) cells.put(DATADATE, Bytes.toBytes(dataDate));
		if(reporter != null) cells.put(REPORTER, Bytes.toBytes(reporter));
		if(checker != null) cells.put(CHECKER, Bytes.toBytes(checker));
		if(bankId != null) cells.put(BANK_ID, Bytes.toBytes(bankId));
		if(ddate != null) cells.put(DATE, Bytes.toBytes(ddate));
		if(xh != null) cells.put(XH, Bytes.toBytes(xh));
		if(xmht != null) cells.put(XMHT, Bytes.toBytes(xmht));
		
		return cells;
	}
}
